3150 #include "types.h"
3151 #include "defs.h"
3152 #include "param.h"
3153 #include "mmu.h"
3154 #include "proc.h"
3155 #include "x86.h"
3156 #include "syscall.h"
3157 
3158 // User code makes a system call with INT T_SYSCALL.
3159 // System call number in %eax.
3160 // Arguments on the stack, from the user call to the C
3161 // library system call function. The saved user %esp points
3162 // to a saved program counter, and then the first argument.
3163 
3164 // Fetch the int at addr from process p.
3165 int
3166 fetchint(struct proc *p, uint addr, int *ip)
3167 {
3168   if(addr >= p->sz || addr+4 > p->sz)
3169     return -1;
3170   *ip = *(int*)(addr);
3171   return 0;
3172 }
3173 
3174 // Fetch the nul-terminated string at addr from process p.
3175 // Doesn't actually copy the string - just sets *pp to point at it.
3176 // Returns length of string, not including nul.
3177 int
3178 fetchstr(struct proc *p, uint addr, char **pp)
3179 {
3180   char *s, *ep;
3181 
3182   if(addr >= p->sz)
3183     return -1;
3184   *pp = (char *) addr;
3185   ep = (char *) p->sz;
3186   for(s = *pp; s < ep; s++)
3187     if(*s == 0)
3188       return s - *pp;
3189   return -1;
3190 }
3191 
3192 // Fetch the nth 32-bit system call argument.
3193 int
3194 argint(int n, int *ip)
3195 {
3196   int x = fetchint(proc, proc->tf->esp + 4 + 4*n, ip);
3197   return x;
3198 }
3199 
3200 // Fetch the nth word-sized system call argument as a pointer
3201 // to a block of memory of size n bytes.  Check that the pointer
3202 // lies within the process address space.
3203 int
3204 argptr(int n, char **pp, int size)
3205 {
3206   int i;
3207 
3208   if(argint(n, &i) < 0)
3209     return -1;
3210   if((uint)i >= proc->sz || (uint)i+size >= proc->sz)
3211     return -1;
3212   *pp = (char *) i;
3213   return 0;
3214 }
3215 
3216 // Fetch the nth word-sized system call argument as a string pointer.
3217 // Check that the pointer is valid and the string is nul-terminated.
3218 // (There is no shared writable memory, so the string can't change
3219 // between this check and being used by the kernel.)
3220 int
3221 argstr(int n, char **pp)
3222 {
3223   int addr;
3224   if(argint(n, &addr) < 0)
3225     return -1;
3226   return fetchstr(proc, addr, pp);
3227 }
3228 
3229 extern int sys_chdir(void);
3230 extern int sys_close(void);
3231 extern int sys_dup(void);
3232 extern int sys_exec(void);
3233 extern int sys_exit(void);
3234 extern int sys_fork(void);
3235 extern int sys_fstat(void);
3236 extern int sys_getpid(void);
3237 extern int sys_kill(void);
3238 extern int sys_link(void);
3239 extern int sys_mkdir(void);
3240 extern int sys_mknod(void);
3241 extern int sys_open(void);
3242 extern int sys_pipe(void);
3243 extern int sys_read(void);
3244 extern int sys_sbrk(void);
3245 extern int sys_sleep(void);
3246 extern int sys_unlink(void);
3247 extern int sys_wait(void);
3248 extern int sys_write(void);
3249 extern int sys_uptime(void);
3250 extern int sys_wait2(void);
3251 extern int sys_nice(void);
3252 
3253 static int (*syscalls[])(void) = {
3254 [SYS_chdir]   sys_chdir,
3255 [SYS_close]   sys_close,
3256 [SYS_dup]     sys_dup,
3257 [SYS_exec]    sys_exec,
3258 [SYS_exit]    sys_exit,
3259 [SYS_fork]    sys_fork,
3260 [SYS_fstat]   sys_fstat,
3261 [SYS_getpid]  sys_getpid,
3262 [SYS_kill]    sys_kill,
3263 [SYS_link]    sys_link,
3264 [SYS_mkdir]   sys_mkdir,
3265 [SYS_mknod]   sys_mknod,
3266 [SYS_open]    sys_open,
3267 [SYS_pipe]    sys_pipe,
3268 [SYS_read]    sys_read,
3269 [SYS_sbrk]    sys_sbrk,
3270 [SYS_sleep]   sys_sleep,
3271 [SYS_unlink]  sys_unlink,
3272 [SYS_wait]    sys_wait,
3273 [SYS_write]   sys_write,
3274 [SYS_uptime]  sys_uptime,
3275 [SYS_wait2]   sys_wait2,
3276 [SYS_nice]    sys_nice,
3277 };
3278 
3279 void
3280 syscall(void)
3281 {
3282   int num;
3283 
3284   num = proc->tf->eax;
3285   if(num >= 0 && num < NELEM(syscalls) && syscalls[num])
3286     proc->tf->eax = syscalls[num]();
3287   else {
3288     cprintf("%d %s: unknown sys call %d\n",
3289             proc->pid, proc->name, num);
3290     proc->tf->eax = -1;
3291   }
3292 }
3293 
3294 
3295 
3296 
3297 
3298 
3299 
